/**
 * Created by 117 on 2016/11/15.
 */
$(function(){
    var path = $('#path').val(),
        bookId = $('#bookId').val(),
        studentId = $('#studentId').val(),
        bookAuthor = $('#bookAuthor').val(),
        bookschool = $('#bookschool').val(),
        schoolId = $('#schoolId').val(),
        bookname = $('#bookname').val();
    console.log("提醒 ： " + bookAuthor,bookschool,bookname);
    //答题按钮
    $('.btn-answer').click(function(){
        var url = $(this).data('url');
        if(schoolId == "4419001"){
        	var warn="正确率超过60%才能通过哦！";
        }
        else{
        	var warn = '正确率超过60%才能获得海洋生物哦！';}
        x0p({
            maxWidth: '520px',
            maxHeight: '292px',
            title: '开始答题',
            text: warn,
            animationType: 'slideUp',
            icon: 'info',
            buttons: [
                {
                    type: 'info',
                    text: '开始答题'
                },
                {
                    type: 'cancel',
                    text: '再读一会'
                }
            ]
        }, function(button) {
        	if (button === 'info') {
                var arr=url.split('animalName=');
                arr[1]=encodeURIComponent(arr[1]);
                if(schoolId =="4419001"){
                	url=arr[0];
                }
                else{
                	url=arr.join("animalName=");
                }
                
                
               
                location.href = url;
            }
        });
    });

    //选择海洋生物按钮
    $('.btn-animal').click(function(){
        var url = $(this).data('url');
        x0p({
            maxWidth: '520px',
            maxHeight: '292px',
            title: '选择海洋生物',
            text: '是否订阅这本书',
            animationType: 'slideUp',
            icon: 'info',
            buttons: [
                {
                    type: 'info',
                    text: '确认'
                },
                {
                    type: 'cancel',
                    text: '取消'
                }
            ]
        }, function(button) {
        	if (button === 'info') {
                var arr=url.split('bookName=');
                arr[1]=encodeURIComponent(arr[1]);
                url=arr.join("bookName=");
                location.href = url;
            }
        });
    });

    //微课按钮
    $('.btn-weike').click(function(){
        var url = $(this).data('url');
        x0p({
            maxWidth: '520px',
            maxHeight: '292px',
            title: '提示',
            text:  '是否观看' + "《" + bookname+ "》" + '微课视频'  + '（' + '作者:' + bookschool + '-' + bookAuthor + ')' ,
            animationType: 'slideUp',
            icon: 'info',
            buttons: [
                {
                    type: 'info',
                    text: '确认'
                },
                {
                    type: 'cancel',
                    text: '取消'
                }
            ]
        }, function(button) {
            if (button === 'info') {
                window.open(url);
            }
        });
    });

    if (studentId !== '') {
        var assessForm = (function(){

            var popup = $('.popup-my');

            //评价表单
            $.ajax({
                type: 'GET',
                url: path + '/sys/query',
                data: {
                    'type': 'parent_add_type'
                },
                cache: false,
                async: false,
                success: function(json) {
                    var li = '';
                    $.each(json.data, function(index, ele){
                        li += '<li><label><input type="checkbox" name="integral" value="'+ this.name +'"><span>'+ this.id + '</span>+'+ this.name +'分</label></li>';
                    });
                    $('.popup-my ul').html(li);
                },
                error: function() {
                    alert('获取评价分数表失败');
                }
            });
            //家长评价按钮
            $('.btn-assess').click(function(){
                popup.slideDown(500);
            });

            //关闭家长评价
            $('.close').click(function(){
                popup.fadeOut('slow');
            });

            //提交家长评价
            $('.form-assess').submit(function(){
                var param = $('.form-assess').serializeArray(),
                    checkbox = $('.popup-my ul').find('input:checked'),
                    number = 0,
                    assess = [],
                    paramlist = [];
                $.each(param, function(){
                    number += parseInt(this.value, 10);
                });
                $.each(checkbox, function(){
                    assess.push($(this).next('span').text());
                    
                });

                if (number === 0) {
                    alert('请至少选择一项');
                }
                else {
                    var strAssess = assess.join('#');
                    paramlist.push('bookId='+bookId);
                    paramlist.push('assess='+strAssess);
                    paramlist.push('integral='+number);
                    paramlist.push('integralType=/parent/personal/add');

                    $.ajax({
                        type: 'POST',
                        url: path + '/book/done',
                        data: paramlist.join('&'),
                        cache: false,
                        async: false,
                        success: function(msg) {
                            $('.close').click();
                            $('.btn-assess').attr('disabled', true).text('已评价');
                            if (msg.code === 200) {
                                x0p('成功', '评价成功,您已成功获取'+number+'点积分');
                            }
                            else if (msg.code === 201) {
                                x0p('成功', '评价成功,但单日评价次数达到上限，无法获取积分');
                            }else if (msg.code === 403) {
                                x0p('成功', '已评价');
                            }
                            $('.subscribe').after('<p class="assess">评语：'+ strAssess +'</p>')
                        },
                        error: function() {
                            alert('提交失败');
                        }
                    })
                }

                return false;
            });
        })();
    }

    $('.btn-back button').click(function(){
        if (document.referrer.indexOf('/bookq/answer') === -1) {
        	if(!document.referrer.indexOf('/task/myTask') === -1){
        		location.href = path + '/task/myTask?type=all';
        	}else{
        	    location.href= path + '/book/index?type=suit&suit=1-2';	
        	}
      
        }
        else {
            location.href = path + '/task/myTask?type=my_reading';
        }
    });
});